var login = {
	doLogin:function(userId,passWord,code){
		var config ={ url:"/api/login",
					  data:{
						  u:userId,
						  p:passWord,
						  v:code
					  },
					  callBackFunc:function(res){
						  //0 暂未审核  1 正常登录 2 被锁定 3用户名或密码错误 4 验证码错误
						  if(res != null){
							  if(res == 0){
								  layer.msg("The user has not yet reviewed !");
							  }
							  if(res == 1){
								  layer.msg("Land successfully !");
								  window.location.href = "/index";
							  }
							  
							  if(res == 2){
								  layer.msg("The user locked !");
							  }
							  
							  if(res == 3){
								  layer.msg("User name or password is incorrect,please log in again!");
							  }
							  if(res == 4){
								  layer.msg("Verification code is incorrect,please log in again!");
							  }
						  }
					  }
					};
		HD.doPost(config);
	},
	//验证码
	getCaptcha:function(){
		var windowUrl = window.URL || window.webkitURL;//处理浏览器兼容性
		  var xhr = new XMLHttpRequest();
		  var url = "/api/captcha";//验证码请求地址
		  xhr.open("GET", url, true);
		  xhr.responseType = "blob";
		  xhr.onload = function () {
		    if (this.status == 200) {
		      var blob = this.response;
		      $("#verificationCode").attr("src", windowUrl.createObjectURL(blob));
		    }
		  }
		  xhr.send();
	},
}

$(function(){
	//点击图片更新验证码
	$("#verificationCode").click(function(){
		login.getCaptcha();
	})
	//默认加载验证码
	login.getCaptcha();
	//重置
	$("#reset").click(function(){
		window.location.href = "/reset";
	})
	//点击注册
	$("#toRegister").click(function(){
		window.location.href = '/index/toRegister';
	})
	//点击登录
	$("#login").click(function(){
		var username = $("input[name='username']").val();
		var password = $("input[name='password']").val();
		var code = $("input[name='code']").val();
		if(username == ''){
			layer.msg("Please input username !");
			return;
		}
		if(!validate.email(username)){
			layer.msg("email is nonlicet ！");
			return;
		}
		if(password == ''){
			layer.msg("Please input password !");
			return;
		}
		if(!validate.pssword(password)){
			layer.msg("password is nonlicet ！At least 6-12 characters,includeing one number/one uppercse letter/one lowerercase letter.");
			return;
		}
		if(code == ''){
			layer.msg("Please input verification code !");
			return;
		}
		if(!validate.isFourDigitNumber(code)){
			layer.msg("It is not four characters !");
			return;
		}
		login.doLogin(username,password,code);
	})
	
})